package top.greatxiaozou.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import top.greatxiaozou.dto.DtoUser;
import top.greatxiaozou.model.Result;
import top.greatxiaozou.pojo.Admin;
import top.greatxiaozou.pojo.User;
import top.greatxiaozou.service.AdminService;
import top.greatxiaozou.service.UserService;
import top.greatxiaozou.service.impl.AdminServiceImpl;
import top.greatxiaozou.utils.ResultsUtils;
import top.greatxiaozou.utils.TokenUtils;
import top.greatxiaozou.utils.UserUtils;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    //用户注册接口
    @PostMapping("/regist")
    public ResponseEntity<Result> registUser(@RequestBody DtoUser newUser){
        int i = userService.registUser(newUser);
        if (i==-1){
            return ResultsUtils.faild("用户名存在",HttpStatus.FORBIDDEN);
        }
        if (i==1){
            return ResultsUtils.success(null,"用户注册成功", HttpStatus.OK);
        }
        return ResultsUtils.faild("未知错误，用户名不能为空！",HttpStatus.FORBIDDEN);
    }

    //用户登录接口
    @GetMapping("/login")
    public ResponseEntity<Result> loginUser(String username, String password, HttpServletResponse response){
        Map<String,Object> map = new HashMap<>();
        Subject subject = SecurityUtils.getSubject();

        String token = TokenUtils.getToken(username, password);
        map.put("token",token);

        UsernamePasswordToken shiroToken = new UsernamePasswordToken(username,password);
        try {
            subject.login(shiroToken);
            return ResultsUtils.success(map,"登录成功",HttpStatus.OK);
        }catch (UnknownAccountException uae){
            return ResultsUtils.faild("用户名不存在",HttpStatus.FORBIDDEN);
        }catch (IncorrectCredentialsException ice){
            return ResultsUtils.faild("密码错误",HttpStatus.FORBIDDEN);
        }

    }
    //登出接口
    @RequestMapping("/logout")
    public ResponseEntity<Result> logout(){
        Subject subject = SecurityUtils.getSubject();
        subject.logout();
        return ResultsUtils.success(null,"登出成功",HttpStatus.OK);
    }

    //返回所有用户信息接口
    @GetMapping("/getinfo")
    public ResponseEntity<Result> getUserInfo(HttpServletRequest request){
        String token = request.getHeader("token");
        if (TokenUtils.isEffective(token)!=null){
            return TokenUtils.isEffective(token);
        }
        String username = (String)TokenUtils.getInfoFromToken(token).get(0);

        User user = userService.selectUserByName(username);
        if (user==null){
            return ResultsUtils.faild("用户被删除",HttpStatus.BAD_REQUEST);
        }
        return ResultsUtils.success(user,"用户查询成功",HttpStatus.OK);
    }

    //更改用户信息接口
    @PostMapping("/update")
    public ResponseEntity<Result> updateUserInfo(HttpServletRequest request, @RequestBody DtoUser dtoUser){
        String token = request.getHeader("token");
        if (TokenUtils.isEffective(token)!=null){
            return TokenUtils.isEffective(token);
        }
        System.out.println(dtoUser);

        String oldName = (String)TokenUtils.getInfoFromToken(token).get(0);
        User user = UserUtils.changeUser(dtoUser);
        System.out.println(user);

        String newToken = TokenUtils.getToken(dtoUser.getUsername(), dtoUser.getPassword());

        if (userService.updateUser(user,oldName)==1){
            return ResultsUtils.success(newToken,"修改成功");
        }
        return ResultsUtils.faild("服务器错误,修改失败",HttpStatus.FORBIDDEN);

    }
}
